﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using CCWin;
using System.Text.RegularExpressions;
using BLL;
using BookModel;

namespace WinFromBook
{
    public partial class FrmAnsAdminInfo : CCSkinMain
    {
        public FrmAnsAdminInfo()
        {
            InitializeComponent();
        }

        #region 封装操作方法
        public string limits { get; set; }//保存管理权限
        public string AName { get; set; }//保存管理名称

        AdminInfoBLL aib = new AdminInfoBLL();
        //检查用户名是否已存在
        public bool CheckAdminName()
        {
            foreach (var item in new BLL.AdminInfoBLL().GetAdminInfo())
            {
                if (item.Aname == textBoxAname.Text.Trim())
                {
                    MessageBox.Show("用户名已存在", "系统提示");
                    return false;
                }
            }
            return true;
        }
        //ListView点击事件
        public void LVClick()
        {
            string ID = "";
            if (this.listViewAdmin.SelectedItems.Count != 0)
            {
                ID = this.listViewAdmin.SelectedItems[0].SubItems[0].Text;
            }
            List<AdminInfo> LA = aib.GetAdminInfo();
            foreach (var item in LA)
            {
                if (item.AID.ToString() == ID)
                {
                    this.textBoxAID.Text = item.AID.ToString();
                    this.textBoxAname.Text = item.Aname;
                    this.textBoxBrith.Text = item.Abrith.Year + "-" + item.Abrith.Month + "-" + item.Abrith.Day;
                    if (item.ASex == "女")
                    {
                        this.radioButtonF.Checked = true;
                    }
                    else
                    {
                        this.radioButtonM.Checked = true;
                    }
                    this.textBoxPassWord.Text = decode(item.Apassword);
                    this.textBoxPhone.Text = item.APhone;
                    this.textBoxRemark.Text = item.Remark;
                    this.textBoxAdress.Text = item.Address;
                    this.comboBoxID.Text = item.AdminlimitsID == 1 ? "超级管理员" : "普通管理员";

                }
            }
        }
        //重置文本框
        public void Rest()
        {
            this.textBoxAdress.Clear();
            this.textBoxAID.Clear();
            this.textBoxAname.Clear();
            this.textBoxBrith.Clear();
            this.textBoxPassWord.Clear();
            this.textBoxPhone.Clear();
            this.textBoxRemark.Clear();
            this.radioButtonM.Checked = true;
            this.comboBoxID.SelectedIndex = 0;
        }
        //注销方法
        public void Cancel()
        {
            if (Examine())
            {
                AdminInfo ai = new AdminInfo
                {
                    AID = int.Parse(textBoxAID.Text),
                    Aname = textBoxAname.Text.Trim(),
                    Abrith = Convert.ToDateTime(textBoxBrith.Text.Trim()),
                    ASex = this.radioButtonM.Checked ? "男" : "女",
                    Apassword = Encrypt(textBoxPassWord.Text),
                    Address = textBoxAdress.Text.Trim(),
                    APhone = textBoxPhone.Text.Trim(),
                    Remark = "该管理员已注销",
                    AdminlimitsID = comboBoxID.Text == "超级管理员" ? 1 : 2
                };
                string sql = aib.updateAdminInfo(ai);
                MessageBox.Show(sql, "系统提示");
                LoadAdminInfo();
                Rest();
            }
        }
        //加载管理员信息
        public void LoadAdminInfo()
        {
            if (limits == "普通管理员")
            {
                this.buttonCenel.Enabled = false;
            }
            this.listViewAdmin.Items.Clear();
            List<AdminInfo> LA = aib.GetAdminInfo();
            foreach (var item in LA)
            {
                ListViewItem lvi = new ListViewItem(item.AID + "");
                lvi.SubItems.Add(item.Aname);
                lvi.SubItems.Add(item.AdminlimitsID == 1 ? "超级管理员" : "普通管理员");
                lvi.SubItems.Add(item.ASex);
                lvi.SubItems.Add(item.APhone);
                lvi.SubItems.Add(item.Remark);
                this.listViewAdmin.Items.Add(lvi);
            }
        }
        //修改管理员事件
        public void UpdateAdmin()
        {
            if (Examine())
            {
                if (limits == "普通管理员")
                {
                    if (textBoxAname.Text.Trim() != AName)
                    {
                        MessageBox.Show("您权限不足", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        return;
                    }
                }
                AdminInfo ai = new AdminInfo
                {
                    AID = int.Parse(textBoxAID.Text),
                    Aname = textBoxAname.Text.Trim(),
                    Abrith = Convert.ToDateTime(textBoxBrith.Text.Trim()),
                    ASex = this.radioButtonM.Checked ? "男" : "女",
                    Apassword = Encrypt(textBoxPassWord.Text),
                    Address = textBoxAdress.Text.Trim(),
                    APhone = textBoxPhone.Text.Trim(),
                    Remark = String.IsNullOrEmpty(textBoxRemark.Text.Trim()) ? "待定" : textBoxRemark.Text.Trim(),
                    AdminlimitsID = comboBoxID.Text == "超级管理员" ? 1 : 2
                };
                string sql = aib.updateAdminInfo(ai);
                MessageBox.Show(sql, "系统提示");
                LoadAdminInfo();
                Rest();
            }
        }
        //判断非空
        public bool Examine()
        {
            if (String.IsNullOrEmpty(textBoxAname.Text.Trim()))
            {
                MessageBox.Show("请输入管理员名称", "系统提示");
                return false;
            }
            if (String.IsNullOrEmpty(textBoxAdress.Text.Trim()))
            {
                MessageBox.Show("请输入住址", "系统提示");
                return false;
            }
            if (!Regex.IsMatch(textBoxBrith.Text.Trim(), @"^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$"))
            {
                MessageBox.Show("年龄格式为：1996-02-08", "系统提示");
                return false;
            }
            if (String.IsNullOrEmpty(textBoxPassWord.Text.Trim()))
            {
                MessageBox.Show("请输入管理员密码", "系统提示");
                return false;
            }


            if (!Regex.IsMatch(textBoxPhone.Text.Trim(), @"^0?1[3458]\d{9}$"))
            {
                MessageBox.Show("请输入正确的联系方式", "系统提示");
                return false;

            }
            return true;
        }
        /// <summary>
        /// 加密方式
        /// </summary>
        /// <param name="Password"></param>
        /// <returns></returns>
        public string Encrypt(string Password)
        {
            //获取字符串长度
            byte[] by = new byte[Password.Length];
            //转换为Byte数组
            by = Encoding.UTF32.GetBytes(Password);
            //转换成字符串
            string bt = Convert.ToBase64String(by);

            byte[] pw = new byte[bt.Length];
            pw = Encoding.UTF8.GetBytes(bt);
            string pwd = Convert.ToBase64String(pw);
            return pwd;
        }
        /// <summary>
        ///解密方式
        /// </summary>
        /// <param name="pass">数据库密码</param>
        /// <returns>返回参数</returns>
        public string decode(string pass)
        {
            try
            {

                //获取数据库字节数组
                byte[] by = Convert.FromBase64String(pass);
                //转换为字符串类型
                string pwd = Encoding.UTF8.GetString(by);
                //转换为Byte数组
                byte[] pw = Convert.FromBase64String(pwd);
                //转换为字符串类型
                string pd = Encoding.UTF32.GetString(pw);
                return pd;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        #endregion

        public void FrmAnsAdminInfo_Load(object sender, EventArgs e)
        {
            LoadAdminInfo();
        }

        private void skinButtonAdd_Click(object sender, EventArgs e)
        {
            UpdateAdmin();
        }

        private void buttonCenel_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void listViewAdmin_Click(object sender, EventArgs e)
        {
            LVClick();
        }
        public bool TextCheck(KeyPressEventArgs e)
        {
            //数字，大写英文，小写英文，删除键
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || ((Keys)(e.KeyChar) == Keys.Back))
            {
                return false;
            }
            return true;

        }
        private void textBoxPhone_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = TextCheck(e);
        }
    }
}
